package ils.services;

import ils.domain.catalog.model.Book;
import ils.domain.catalog.model.BookRepository;
import ils.domain.issue.model.Issue;
import ils.domain.issue.model.Reserve;
import ils.domain.model.User;
import ils.domain.model.UserRepository;
import ils.domain.ugc.model.Review;
import ils.util.Util;
import ils.web.model.WBook;
import ils.web.model.MyIssue;
import ils.web.model.ReservationDetails;
import ils.web.model.WReview;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CirculationCommonImpl implements CirculationCommon
{
    private BookRepository bookRepository;
    private UserRepository userRepository;
    @Override
    public List<MyIssue> convertIssues(List<Issue> issues)
    {
        throw new UnsupportedOperationException("Not implemented");
    }

    @Override
    public List<WBook> convertBooks(List<Book> books)
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<ReservationDetails> convertReservation(List<Reserve> reserves)
    {
        List<ReservationDetails> reservationDetailses = new ArrayList<ReservationDetails>();
        if(reserves == null || reserves.size() == 0) return reservationDetailses;
        List<Integer> userIds = new ArrayList<Integer>();
        List<Integer> bookIds = new ArrayList<Integer>();
        for(Reserve reserve : reserves)
        {
            userIds.add(reserve.getUserId());
            bookIds.add(reserve.getBookId());
        }
        Map<Integer, User> users = userRepository.getUsers(userIds);
        Map<Integer, Book> books = bookRepository.getBooks(bookIds);
        for(Reserve reserve : reserves)
        {
            ReservationDetails reservationDetails = new ReservationDetails();
            Util.copy(reservationDetails, reserve);
            reservationDetails.setRequestor(users.get(reserve.getUserId()).getFirstName());
            Book book = books.get(reserve.getBookId());
            reservationDetails.setBookName(book.getTitle());
            reservationDetails.setAvailable(book.getAvailableCount());
            reservationDetails.setTotal(book.getTotalCount());
            reservationDetailses.add(reservationDetails);
            
        }
        return reservationDetailses;
    }

    @Override
    public List<WReview> convertReview(List<Review> reviews)
    {
        // TODO Auto-generated method stub
        return null;
    }

    public void setBookRepository(BookRepository bookRepository)
    {
        this.bookRepository = bookRepository;
    }

    public void setUserRepository(UserRepository userRepository)
    {
        this.userRepository = userRepository;
    }

}
